//
//  LdalypJLCFListPayOrderViewControllerRSNbkl.m
//  qcb_ios
//
//  Created by Ji on 2018/9/3.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "LdalypJLCFListPayOrderViewControllerRSNbkl.h"
#import "XvmSEfJLCFListRequestdkZIyb.h"
//#import "ListWelfareGroupViewController.h"
#import "BwDDHrJLCFListPayCompleteViewControllerHlxtCn.h"
#import "YgMWMCJLCFFuyouInfoModelNQcnsr.h"

@interface LdalypJLCFListPayOrderViewControllerRSNbkl ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *aSHWXpmsgTextFieldgHtMSl;
@property (nonatomic, strong) UIButton *msgBtn;

@property (nonatomic, strong) NSTimer *qdZZsdcountTimereVpqYS;
@property (nonatomic, assign) int count;



@end

static int const showtime = 60;

@implementation LdalypJLCFListPayOrderViewControllerRSNbkl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addNavigationBar];
    self.navigationBar.title = @"确认订单";
    
    self.view.backgroundColor = QTZ_Gray237;
    [self configView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configView
{
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 13 + NAVBAR_HEIGHT, FULL_WIDTH, 152)];
    bgView.backgroundColor = QTZ_White;
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
 
    UILabel *amountTitleLabel = [UILabel labelWithText:@"订单金额" textColor:QTZ_Gray51 font:FONT(16)];
    amountTitleLabel.left = 13;
    amountTitleLabel.centerY = 27;
    [bgView addSubview:amountTitleLabel];
    
    UILabel *GAvcnIamountLabelMphKiM = [UILabel new];
    GAvcnIamountLabelMphKiM.textColor = QTZ_Black;
    GAvcnIamountLabelMphKiM.font = FONT(16);
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@元", self.amount]];
    [attributeString addAttribute:NSForegroundColorAttributeName value:QTZ_RED range:NSMakeRange(0, self.amount.length)];
    GAvcnIamountLabelMphKiM.attributedText = attributeString;
    [GAvcnIamountLabelMphKiM sizeToFit];
    GAvcnIamountLabelMphKiM.right = FULL_WIDTH - 13;
    GAvcnIamountLabelMphKiM.centerY = 27;
    [bgView addSubview:GAvcnIamountLabelMphKiM];
    [amountTitleLabel.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, amountTitleLabel.height + 12) toPoint:CGPointMake(GAvcnIamountLabelMphKiM.right, amountTitleLabel.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
    UILabel *welfareTitleLabel = [UILabel labelWithText:@"优惠券" textColor:QTZ_Gray51 font:FONT(16)];
    welfareTitleLabel.left = 13;
    welfareTitleLabel.centerY = 27 + 54;
    [bgView addSubview:welfareTitleLabel];
    
//    UIImageView *qTVbTrarrowImgwIPROn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_list_detail_arrow"]];
//    qTVbTrarrowImgwIPROn.right = FULL_WIDTH - 13;
//    qTVbTrarrowImgwIPROn.centerY = welfareTitleLabel.centerY;
//    [bgView addSubview:qTVbTrarrowImgwIPROn];
    
    UILabel *XpLfSfwelfareLabelCbxWjE = [UILabel new];
    XpLfSfwelfareLabelCbxWjE.left = GAvcnIamountLabelMphKiM.left ;
    XpLfSfwelfareLabelCbxWjE.top = welfareTitleLabel.top;
    XpLfSfwelfareLabelCbxWjE.textColor = QTZ_Gray51;
    XpLfSfwelfareLabelCbxWjE.font = FONT(16);
    XpLfSfwelfareLabelCbxWjE.textAlignment = NSTextAlignmentRight;
    if (self.welfare) {
        XpLfSfwelfareLabelCbxWjE.text = [NSString stringWithFormat:@"%@元",self.welfare.hbmoney] ;
    }else{
        XpLfSfwelfareLabelCbxWjE.text = @"未使用";
    }
    
    [XpLfSfwelfareLabelCbxWjE sizeToFit];
//    XpLfSfwelfareLabelCbxWjE.userInteractionEnabled = YES;
//    [XpLfSfwelfareLabelCbxWjE bk_whenTapped:^{
//        ListWelfareGroupViewController *vc = [ListWelfareGroupViewController new];
//        vc.id = self.order.borrowId;
//        vc.amount = self.amount;
//        vc.welfareStr = ^(NSString *welfareStr, NSString *welfareId) {
//            XpLfSfwelfareLabelCbxWjE.text = welfareStr;
//            self.welfareId = welfareId;
//        };
//        [[[JLCFApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
//    }];
    [bgView addSubview:XpLfSfwelfareLabelCbxWjE];
    [welfareTitleLabel.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, welfareTitleLabel.height + 12) toPoint:CGPointMake(FULL_WIDTH -13, welfareTitleLabel.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
    UILabel *bankTitleLabel = [UILabel labelWithText:@"银行卡号" textColor:QTZ_Gray51 font:FONT(16)];
    bankTitleLabel.left = 13;
    bankTitleLabel.centerY = 27 + 108;
    [bgView addSubview:bankTitleLabel];
    
    UILabel *bankLabel = [UILabel labelWithText:[NSString stringWithFormat:@"%@(尾号%@)", self.cardinfo.bankName, self.cardinfo.shortCard] textColor:QTZ_Gray51 font:FONT(16)];
    bankLabel.right = FULL_WIDTH - 13;
    bankLabel.centerY = 27 + 108;
    [bgView addSubview:bankLabel];
    [bankTitleLabel.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, bankTitleLabel.height + 12) toPoint:CGPointMake(bankLabel.right,  bankTitleLabel.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
//    UILabel *msgLabel = [UILabel labelWithText:@"验证码" textColor:QTZ_Gray51 font:FONT(16)];
//    msgLabel.left = 13;
//    msgLabel.centerY = 27 + 108 + 54;
//    [bgView addSubview:msgLabel];
    
    
//    self.aSHWXpmsgTextFieldgHtMSl = [[UITextField alloc] initWithFrame:CGRectMake(msgLabel.right + 30, 0, FULL_WIDTH - msgLabel.right - 30 - 13, 30)];
//    self.aSHWXpmsgTextFieldgHtMSl.centerY = msgLabel.centerY;
//    self.aSHWXpmsgTextFieldgHtMSl.textAlignment = NSTextAlignmentRight;
//    self.aSHWXpmsgTextFieldgHtMSl.font = FONT(13);
//    self.aSHWXpmsgTextFieldgHtMSl.delegate = self;
//    self.aSHWXpmsgTextFieldgHtMSl.placeholder = @"请输入您的验证码";
//    self.aSHWXpmsgTextFieldgHtMSl.keyboardType = UIKeyboardTypeNumberPad;
//    self.aSHWXpmsgTextFieldgHtMSl.returnKeyType = UIReturnKeyDone;
//    self.aSHWXpmsgTextFieldgHtMSl.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [bgView addSubview:self.aSHWXpmsgTextFieldgHtMSl];
//
//    self.msgBtn = [UIButton new];
//    self.msgBtn.titleLabel.font = FONT(9);
//    [self.msgBtn setTitleColor:QTZ_RED forState:0];
//    [self.msgBtn setTitle:@"发送验证码" forState:0];
//    [self.msgBtn sizeToFit];
//    [self.msgBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
//    self.aSHWXpmsgTextFieldgHtMSl.rightView = self.msgBtn;
//    self.aSHWXpmsgTextFieldgHtMSl.rightViewMode = UITextFieldViewModeAlways;
    
    UIButton *payBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, bgView.bottom + 80, FULL_WIDTH - 76, 40)];
    payBtn.layer.cornerRadius = 5;
    payBtn.layer.masksToBounds = YES;
    payBtn.titleLabel.font = FONT(15);
    [payBtn setBackgroundColor:QTZ_RED];
    [payBtn setTitleColor:QTZ_White forState:0];
    [payBtn setTitle:@"立即支付" forState:0];
    [payBtn addTarget:self action:@selector(handlePay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payBtn];
    
    UILabel *agreeLabel = [UILabel labelWithText:@"我已同意" textColor:QTZ_Gray51 font:FONT(10)];
    agreeLabel.centerX = FULL_WIDTH / 2;
    agreeLabel.top = payBtn.bottom + 20;
    [self.view addSubview:agreeLabel];
    
    UILabel *LFUwgeprotocolLabelRuOeSa = [UILabel labelWithText:@"《出借咨询与服务协议》" textColor:RGB(250, 143, 5) font:FONT(10)];
    LFUwgeprotocolLabelRuOeSa.centerX = FULL_WIDTH / 2;
    LFUwgeprotocolLabelRuOeSa.top = agreeLabel.bottom + 13;
    LFUwgeprotocolLabelRuOeSa.userInteractionEnabled = YES;
    [LFUwgeprotocolLabelRuOeSa bk_whenTapped:^{
        JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
        webVC.url = QTZ_WEB_URL(@"loan_agreement.html");
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
    }];
    [self.view addSubview:LFUwgeprotocolLabelRuOeSa];
    
    UILabel *edHyphtipsLabelTVYPVW = [UILabel labelWithText:@"《网络借贷风险和禁止性行为提示书》" textColor:RGB(250, 143, 5) font:FONT(10)];
    edHyphtipsLabelTVYPVW.centerX = FULL_WIDTH / 2;
    edHyphtipsLabelTVYPVW.top = LFUwgeprotocolLabelRuOeSa.bottom + 10;
    edHyphtipsLabelTVYPVW.userInteractionEnabled = YES;
    [edHyphtipsLabelTVYPVW bk_whenTapped:^{
        JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
        webVC.url = QTZ_WEB_URL(@"notification.html");
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
    }];
    [self.view addSubview:edHyphtipsLabelTVYPVW];
    
    
}

- (void)handlePay
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.amount forKey:@"amt"];
//    [params setSafeObject:self.proid forKey:@"borrowId"];s
    [params setSafeObject:[NSNumber numberWithInteger:[self.proid integerValue]] forKey:@"borrowId"];

//    if (self.welfare) {
        [params setSafeObject:self.welfare.hbid forKey:@"hbid"];
//    }
    [params setSafeObject:self.cardinfo.realName forKey:@"name"];
    [params setSafeObject:self.cardinfo.realBankCardNo forKey:@"bankCard"];
    [params setSafeObject:self.cardinfo.realPid forKey:@"pid"];
    [params setSafeObject:@"app" forKey:@"payChannel"];
    [params setSafeObject:CHANNEL forKey:@"channel"];
    
    [XvmSEfJLCFListRequestdkZIyb payCompleteWithParams:params loadingInView:self.view paySuccess:^(YgMWMCJLCFFuyouInfoModelNQcnsr *data) {
        
        JLCFBaseWebViewController *webvc = [[JLCFBaseWebViewController alloc]init];
        webvc.url = data.url;
        webvc.fuyouinfo = data;
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webvc animated:YES];
    } payFailure:^(NSString *payfail) {
        
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

//获取验证码
- (void)getVerifyCode
{
    [XvmSEfJLCFListRequestdkZIyb sendCodeWithParams:nil loadingInView:self.view success:^{
        [self handleVerifyBtn];
        [self showNotice:@"验证码发送成功"];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
   
}

- (void)handleVerifyBtn
{
    _count = showtime;
    [[NSRunLoop mainRunLoop] addTimer:self.qdZZsdcountTimereVpqYS forMode:NSRunLoopCommonModes];
}

- (NSTimer *)qdZZsdcountTimereVpqYS
{
    if (!_qdZZsdcountTimereVpqYS) {
        _qdZZsdcountTimereVpqYS = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _qdZZsdcountTimereVpqYS;
}

- (void)countDown
{
    _count --;
    [self.msgBtn setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:QTZ_Gray173 forState:0];
    self.msgBtn.userInteractionEnabled = NO;
    if (_count == 0) {
        [self.msgBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:[UIColor blueColor] forState:0];
        self.msgBtn.userInteractionEnabled = YES;
        [self dismiss];
    }
    [self.msgBtn sizeToFit];
}

- (void)dismiss
{
    [self.qdZZsdcountTimereVpqYS invalidate];
    self.qdZZsdcountTimereVpqYS = nil;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * ZLmcEJshapeLayerWDClkp = [CAShapeLayer layer];
    ZLmcEJshapeLayerWDClkp.strokeColor = color.CGColor;
    ZLmcEJshapeLayerWDClkp.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    ZLmcEJshapeLayerWDClkp.path = bezierPath.CGPath;
    /// 线宽度
    ZLmcEJshapeLayerWDClkp.lineWidth = borderWidth;
    return ZLmcEJshapeLayerWDClkp;
}

@end

